package com.mofang.activity.user;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.activity.MofangCommonUpdateView;
import com.mofang.controller.UserController;
import com.mofang.utils.MoFangSession;
import com.pactera.framework.exception.IException;
import com.pactera.framework.util.ToastHelper;

public class UserChangePasswordActivity extends BaseTitleViewActiviey implements
		OnClickListener {

	private EditText psw_enter_edit;// 输入密码
	private EditText psw_check_edit;// 确认密码
	private EditText psw_captcha_edit;// 输入手机验证码
	private LinearLayout psw_captcha_btn;// 发送验证码
	private Button btn_user_setting_change_psw;// 确定
	private UserController userController;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_user_change_password);
		super.onCreate(savedInstanceState);
		setNavigationTitle("修改密码");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
		userController = new UserController();
	}

	@Override
	protected void initView() {
		super.initView();
		// TODO Auto-generated method stub
		psw_enter_edit = (EditText) findViewById(R.id.user_change_psw_enter_edit);

		psw_check_edit = (EditText) findViewById(R.id.user_change_psw_check_edit);

		psw_captcha_edit = (EditText) findViewById(R.id.user_change_psw_captcha_edit);

		psw_captcha_btn = (LinearLayout) findViewById(R.id.user_change_psw_captcha_btn);

		btn_user_setting_change_psw = (Button) findViewById(R.id.btn_user_setting_change_psw);
	}

	@Override
	protected void setListener() {
		super.setListener();
		psw_captcha_btn.setOnClickListener(this);
		btn_user_setting_change_psw.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		String phoneNo = MoFangSession.getAccount();
		switch (v.getId()) {
		case R.id.btn_user_setting_change_psw:
			// 确定
			String newPassword=psw_enter_edit.getText().toString();
			String verificationCode="";
			userController.changeUserPassword(new ChangeUserPassswordUpdateView(mContext),
					phoneNo, newPassword,verificationCode);

			break;

		case R.id.user_change_psw_captcha_btn:
			userController.sendVerificationCode(new VerificationCodeUpdateView(
					mContext), phoneNo);
			break;

		default:
			break;
		}
	}

	private class VerificationCodeUpdateView extends
			MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public VerificationCodeUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(UserChangePasswordActivity.this,
					null, "正在发送验证码", true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			// result=false;
			progressDialog.dismiss();
			if (result) {
				ToastHelper.getInstance().showLongMsg("发送成功");
			} else {
				viewHelper.showBTNDialog("发送失败，请重试");
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}
	private class ChangeUserPassswordUpdateView extends MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public ChangeUserPassswordUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(UserChangePasswordActivity.this, null,
					getResources().getString(R.string.handle_in_process), true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			//result=false;
			progressDialog.dismiss();
			if (result) {
				ToastHelper.getInstance().showLongMsg(getResources().getString(R.string.handle_success));
			} else {
				viewHelper.showBTNDialog(getResources().getString(R.string.handle_failure));
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}
}
